<template>
	<div class="register-wrap">
		<div class="register-title">用户注册</div>
		<div class="register-container">
			<el-form :model="registerForm" :rules="rules" ref="registerForm">
				<el-form-item label="用户名" prop="username">
					<el-input v-model="registerForm.username"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="registerForm.password" type="password"></el-input>
				</el-form-item>
				<el-form-item label="确认密码" prop="confirmPassword">
					<el-input v-model="registerForm.confirmPassword" type="password"></el-input>
				</el-form-item>
				<el-form-item label="选择角色" prop="role">
					<el-select v-model="registerForm.role" placeholder="请选择角色">
						<el-option v-for="item in roleOptions" :key="item.value" :label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="手机号" prop="phone">
					<el-input v-model="registerForm.phone"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="registerForm.email"></el-input>
				</el-form-item>
				<div class="register-btn">
					<el-button type="primary" @click="submitRegister">注册</el-button>
				</div>
			</el-form>
		</div>
	</div>
</template>

<script>
	import {
		mixin
	} from "../../mixins/index";
	import {
		getRegisterStatus,getUserRoles
	} from "../../api/index.js";
	export default {
		mixins: [mixin],
		data() {
			const validatePassword = (rule, value, callback) => {
				if (value != this.registerForm.password) {
					callback(new Error("两次输入的密码不一致！"));
				} else {
					callback();
				}
			};


			return {
				registerForm: {
					username: '',
					password: '',
					confirmPassword: '',
					role: '',
					phone: '',
					email: ''
				},
				roleOptions: [{
						label: '医护人员',
						value: 'doctor'
					},
					{
						label: '普通用户',
						value: 'user'
					}
				],
				rules: {
					username: [{
							required: true,
							message: '请输入用户名',
							trigger: 'blur'
						},
						{
							min: 3,
							max: 16,
							message: '长度在3到16个字符之间',
							trigger: 'blur'
						}
					],
					password: [{
							required: true,
							message: '请输入密码',
							trigger: 'blur'
						},
						{
							min: 6,
							max: 16,
							message: '长度在6到16个字符',
							trigger: 'blur'
						}
					],
					confirmPassword: [{
							required: true,
							message: '请再次输入密码',
							trigger: 'blur'
						},
						{
							validator: validatePassword,
							trigger: 'blur'
						}
					],
					role: [{
						required: true,
						message: '请选择角色',
						trigger: 'change'
					}],
					phone: [{
							required: true,
							message: '请输入手机号',
							trigger: 'blur'
						},
						{
							len: 11,
							message: '请输入正确的手机号',
							trigger: 'blur'
						}
					],
					email: [{
							required: true,
							message: '请输入邮箱',
							trigger: 'blur'
						},
						{
							type: 'email',
							message: '请输入正确的邮箱',
							trigger: 'blur'
						}
					]
				}
			};
		},
		created() {
			this.fetchRoles(); // 组件创建时获取角色列表
		},
		methods: {
			submitRegister() {
				this.$refs.registerForm.validate((valid) => {
					if (valid) {
						this.registerUser();
					} else {
						return false;
					}
				});
			},
			// 获取角色列表的方法
			async fetchRoles() {
				try {
					const response = await getUserRoles();
					if(response.code == 1){
						this.roleOptions = response.roles.map(role =>{
							const roleMappings = {
								'医护人员':'doctor',
								'普通用户':'user'
							};
							return {
								label: role,
								value: roleMappings[role]
							};
						});
					} else {
						console.error('获取角色列表失败：',response);
						this.notify('获取角色列表失败');
					}
				} catch (error) {
					console.error('获取角色列表失败:', error);
					this.notify('获取角色列表失败');
				}
			},
			async registerUser() {
				let params = new URLSearchParams();
				params.append("username", this.registerForm.username);
				params.append("password", this.registerForm.password);
				params.append("role", this.registerForm.role);
				params.append("phone", this.registerForm.phone);
				params.append("email", this.registerForm.email);
				getRegisterStatus(params)
					.then((res) => {
						if (res.code == 1) {
							this.notify(res.msg, "success");
							this.$router.push("/login");
						} else {
							this.notify(res.msg, "error");
						}
					});
			}
		}
	};
</script>

<style>
	.register-wrap {
		position: absolute;
		width: 300px;
		height: 800px;
		margin-left: 38%;
	}

	.register-title {
		text-align: center;
		color: black;
		font-size: 36px;
	}

	.register-btn {
		text-align: center;
	}

	.register-btn .el-button {
		width: 100%;
		height: 36px;
		padding: 0;
	}
</style>